﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrapeCity.ActiveReports;
using GrapeCity.ActiveReports.Viewer.Win;
using System.Windows.Forms;
using GrapeCity.ActiveReports.Design;
using System.Data;
using System.Data.OleDb;

namespace ActiveReportsDemo
{
    class CommonFunction
    {


        public static PageReport CreatePageReport(string name, Viewer viewer, string theme)
        {
            PageReport report1 = new PageReport(new System.IO.FileInfo(string.Format(@"Reports\{0}", name)));
            if (!string.IsNullOrEmpty(theme))
            {
                report1.Report.Themes.Clear();
                report1.Report.Themes.Add(theme);
            }
            GrapeCity.ActiveReports.Document.PageDocument document1 = new GrapeCity.ActiveReports.Document.PageDocument(report1);            
            viewer.LoadDocument(document1);            
            return report1;
        }

        public static SectionReport CreateSectionReportRpx(string name, Viewer viewer)
        {
            SectionReport report1 = new SectionReport();
            report1.LoadLayout(string.Format(@"Reports\{0}", name));
            report1.Document.Printer.PrinterName = "";
            viewer.Document = report1.Document;
            report1.Run(true);
            return report1;
        }

        public static void CreateSectionReportRpt(string name, Viewer viewer)
        {
            viewer.Toolbar.ToolStrip.Items[3].Visible = false;
            viewer.Toolbar.ToolStrip.Items[4].Visible = false;
            viewer.Document.Load(string.Format(@"Reports\{0}", name));
        }

        //public static SectionReport CreateSectionReportCode(string name, Viewer viewer)
        //{            
        //    SectionReport report1 = new SectionReport();
        //    report1.LoadLayout(string.Format(@"Reports\{0}", name));
        //    report1.Document.Printer.PrinterName = "";
        //    viewer.Document = report1.Document;
        //    report1.Run(false);
        //    return report1;
        //}

        public static string GetTheme(int theme, string type)
        {
            switch (type)
            {
                case "Page":
                    return string.Format(@"..\Theme\Style{0}.rdlx-theme",theme);                    
                case "Section":
                    return @"..\Theme\DefaultStyle.reportstyle";
                default:
                    return "";
            }
        }

        public static string getDescription(String reportid)
        {
            if (reportid.Contains("\\"))
            {
                reportid = reportid.Substring(reportid.LastIndexOf("\\") + 1);
            }
            string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data\ReportsResources.mdb;Persist Security Info=False";
            DataTable dt = new DataTable();
            string sql = "select Description from ReportDescription where ReportID = @id";
            OleDbParameter[] param = { new OleDbParameter("@id", OleDbType.VarChar) };
            param[0].Value = reportid;

            using (OleDbDataAdapter oda = new OleDbDataAdapter(sql, conStr))
            {
                if (param != null && param.Length != 0)
                {
                    oda.SelectCommand.Parameters.AddRange(param);
                }
                if (new OleDbConnection().State == ConnectionState.Closed)
                {
                    new OleDbConnection(conStr).Open();
                }
                oda.Fill(dt);
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("<html><head><link href='http://demo.gcpowertools.com.cn/ActiveReports/ASPNET/ControlExplorer/css/style.css' rel='stylesheet' type='text/css'></head>");
            sb.Append("<body><div class='reportintroduce'>");
            string content = dt.Rows[0].ItemArray[0].ToString();
            content = content.Replace(".aspx", ".aspx?utm_source=Demo&utm_medium=activereports&utm_term=controlexplorer&utm_content=blog&utm_campaign=win");
            sb.Append(content);
            sb.Append("</div></body></html>");

            return sb.ToString();
        }
      
    }


}
